package com.op.parcel.manage.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.op.parcel.boot.support.Condition;
import com.op.parcel.boot.support.Query;
import com.op.parcel.common.api.R;
import com.op.parcel.manage.modules.sys.entity.User;
import com.op.parcel.manage.modules.sys.service.IRoleService;
import com.op.parcel.manage.modules.sys.service.IUserService;
import com.op.parcel.manage.modules.sys.vo.UserVO;
import com.op.parcel.manage.modules.sys.wrapper.UserWrapper;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author xuan
 * @since 2019-01-07
 */
@RestController
@AllArgsConstructor
@RequestMapping("user")
@Api(value = "", tags = "接口")
public class UserController {

	private IUserService userService;

	private IRoleService roleService;

	private PasswordEncoder passwordEncoder;


	/**
	 * 详情
	 */
	@GetMapping("detail")
	@ApiOperation(value = "详情", notes = "传入user", position = 1)
	public R<UserVO> detail(User user) {
		User detail = userService.getOne(Condition.getQueryWrapper(user));
		UserWrapper userWrapper = new UserWrapper(roleService);
		return R.success(userWrapper.entityVO(detail));
	}


	/**
	 * 修改密码
	 */
	@PostMapping("pwd")
	@ApiOperation(value = "修改密码", notes = "传入user", position = 1)
	public R pwd(@RequestBody @Validated(User.update.class) User user) {
		User u = new User();
		u.setUsername(user.getUsername());
		userService.updatePwd(user);
		return R.success("修改密码成功");
	}


	/**
	 * 分页
	 */
	@GetMapping("list")
	@ApiOperation(value = "分页", notes = "传入user", position = 2)
	public R list(@RequestParam(required = false) Map<String, Object> req, User user, Query query) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isNotEmpty(user.getUsername())) {
			queryWrapper.lambda().like(User::getUsername, user.getUsername());
		}
		if (StringUtils.isNotEmpty(user.getMobile())) {
			queryWrapper.lambda().like(User::getMobile, user.getMobile());
		}
		if (StringUtils.isNotEmpty(user.getEmail())) {
			queryWrapper.lambda().like(User::getEmail, user.getEmail());
		}
		if (user.getDelFlag() != null) {
			queryWrapper.lambda().eq(User::getDelFlag, user.getDelFlag());
		}
		IPage<User> pages = userService.page(Condition.getPage(query), queryWrapper);
		UserWrapper userWrapper = new UserWrapper(roleService);
		return R.success(userWrapper.pageVO(pages));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("saveOrUpdate")
	@ApiOperation(value = "新增或修改", notes = "传入user", position = 3)
	public R saveOrUpdate(@Validated @RequestBody User user,@RequestParam(required = false) List<String> roleIds) {
		userService.saveOrUpdateUser(user, roleIds);
		return R.success("操作成功");
	}


	/**
	 * 删除
	 */
	@PostMapping("remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids", position = 4)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam List<String> ids) {
		return R.status(userService.removeByIds(ids));
	}


}
